package com.rongke.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rongke.commons.JsonResp;
import com.rongke.enums.EachTypeEnum;
import com.rongke.model.EachPicture;
import com.rongke.model.ExperienceGoods;
import com.rongke.model.Goods;
import com.rongke.service.EachPictureService;
import com.rongke.service.ExperienceGoodsService;
import com.rongke.service.GoodsService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ExperienceGoodsController
 * @体验众测Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/experienceGoods")
@Transactional
@CrossOrigin(origins = "*", maxAge = 3600)
public class ExperienceGoodsController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private ExperienceGoodsService experienceGoodsService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private EachPictureService eachPictureService;

    /**
     * @添加体验众测
     * @param experienceGoods
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addExperienceGoods(@RequestBody ExperienceGoods experienceGoods ){
        log.debug("添加体验众测");
        experienceGoodsService.insert(experienceGoods);
        return JsonResp.ok("");
    }
    /**
     * @批量添加体验众测
     * @param experienceGoods
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/addBatch", method = RequestMethod.POST)
    public JsonResp addExperienceGoodsBatch(@RequestBody List<ExperienceGoods> experienceGoods ){
        log.debug("批量添加体验众测");
        experienceGoodsService.insertBatch(experienceGoods);
        return JsonResp.ok("");
    }

    /**
     * @修改体验众测
     * @param experienceGoods
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateExperienceGoods(@RequestBody ExperienceGoods experienceGoods){
        log.debug("修改体验众测");
        experienceGoodsService.updateById(experienceGoods);
        return JsonResp.ok(experienceGoods);
    }
    /**
     * @根据id查找体验众测
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectExperienceGoods(Long id){
        log.debug("查找体验众测");
        ExperienceGoods experienceGoods = experienceGoodsService.selectById(id);
        return JsonResp.ok(experienceGoods);
    }

    /**
     * @根据条件查找体验众测
     * @param experienceGoods
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST)
    public JsonResp selectExperienceGoodsByCondition(@RequestBody ExperienceGoods experienceGoods){
        log.debug("根据条件查找体验众测");
        if(experienceGoods==null)
            return JsonResp.toFail("参数不能为空");
        List<ExperienceGoods> experienceGoodsList=experienceGoodsService.selectByCondition(experienceGoods);
        return JsonResp.ok(experienceGoodsList);
    }

    /**
     * @分页查找体验众测商品
     * @param page
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectPage", method = RequestMethod.GET)
    public JsonResp selectExpGoods(Page page){
        log.debug("分页查找体验众测商品");
        Map map=new HashMap();
        Integer startIndex=(page.getCurrent()-1)*page.getSize();
        map.put("startIndex",startIndex);
        map.put("size",page.getSize());
        map.put("zz","zz");//app端不接收三无（图片、价格、数量）商品
        List<ExperienceGoods> origiGoodsPage = experienceGoodsService.selectExpGoodsList(map);
        if(!origiGoodsPage.isEmpty()){
            origiGoodsPage.forEach(e->{
                goodsService.fetchGoodsInventoryStatus(e.getGoods());//判断库存状态
            });
        }
        int total=experienceGoodsService.selectCount(null);
        total=origiGoodsPage.size();
        page.setRecords(origiGoodsPage);
        page.setTotal(total);
        return JsonResp.ok(page);
    }

    /**
     * @后台分页查找全部体验众测商品
     * @param page
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectPageAdmin", method = RequestMethod.GET)
    public JsonResp selectExpGoodsAdmin(Page page){
        log.debug("后台分页查找全部体验众测商品");
        Map map=new HashMap();
        Integer startIndex=(page.getCurrent()-1)*page.getSize();
        map.put("startIndex",startIndex);
        map.put("size",page.getSize());
        List<ExperienceGoods> goodsPage = experienceGoodsService.selectExpGoodsList(map);
        int total=experienceGoodsService.selectCount(null);
        page.setRecords(goodsPage);
        page.setTotal(total);
        return JsonResp.ok(page);
    }

    /**
     * @分页查找体验众测商品(微信页面使用)
     * @param page
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectwxPage", method = RequestMethod.GET)
    public JsonResp selectwxPage(Page page){
        log.debug("分页查找体验众测商品(微信页面使用)");
        Map map=new HashMap();
        Integer startIndex=(page.getCurrent()-1)*page.getSize();
        map.put("startIndex",startIndex);
        map.put("size",page.getSize());
        List<ExperienceGoods> goodsPage = experienceGoodsService.selectExpGoodsList(map);
        EntityWrapper<EachPicture>  ew=new EntityWrapper<>();
        ew.eq("each_type", EachTypeEnum.EXPECIENCEGOODS.getType());
        EachPicture eachPicture=eachPictureService.selectOne(ew);
        Map map1=new HashMap();
        map1.put("eachPicture",eachPicture);
        map1.put("goodsPage",goodsPage);
        return JsonResp.ok(map1);
    }

    /**
     * 获取某产品线下已发布未分组的商品集合（交给web客户端分页）
     * @param goodsType
     * @return
     */
    @RequestMapping(value = "/getNotGroupedGoodsList",method = RequestMethod.GET)
    public JsonResp getNotGroupedGoodsList(int goodsType){
        log.debug("获取某产品线下未分组的商品集合");
        EntityWrapper<Goods> ew=new EntityWrapper<>();
        //goods_status 商品状态 1代表只上传未发布 2代表正式发布上架 3 下架
        ew.eq("goods_status",2).eq("goods_type",goodsType);
        List<Goods> goodsList=goodsService.selectList(ew);
        //查询已有的商品
        List<ExperienceGoods> popGoodses=experienceGoodsService.selectExpGoodsList(new HashMap());
        if(!popGoodses.isEmpty()){
            for(int i=0,y=popGoodses.size();i<y;i++){
                Goods goods=popGoodses.get(i).getGoods();
                if(goodsList.contains(goods)){
                    goodsList.remove(goods);
                }
            }
        }

        return JsonResp.ok(goodsList);
    }

    /**
     * @删除
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/deleteExGoods", method = RequestMethod.GET)
    public JsonResp deleteExGoods(Long id){
        log.debug("删除");
        experienceGoodsService.deleteById(id);
        return JsonResp.ok();
    }
    /**
     * @设置和取消首页显示
     * @param id
     * @param isHome
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/updIsHome", method = RequestMethod.GET)
    public JsonResp updIsHome(Long id,Integer isHome){
        log.debug("设置和取消首页显示");
        ExperienceGoods ng = experienceGoodsService.selectById(id);
        ng.setIsHome(isHome);
        experienceGoodsService.updateById(ng);
        return JsonResp.ok();
    }

    /**
     * @抢光了
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/updNot", method = RequestMethod.GET)
    public JsonResp updNot(Long id){
        log.debug("设置和取消首页显示");
        ExperienceGoods ng = experienceGoodsService.selectById(id);
        ng.setNum(0);
        experienceGoodsService.updateById(ng);
        return JsonResp.ok();
    }
}
